/*
 * SessionIdentInconsistenciaBean.java
 *
 * Created on June 5, 2007, 10:55 AM
 * Copyright fsanchez
 */
package gestionportletidentificarinconsistencia;

import com.src.computec.inconsistencias.constantes.ConstantesGenerales;
import com.src.computec.inconsistencias.delegate.InconsistenciasDelegate;
import com.src.computec.inconsistencias.exception.InconsistenciasException;
import com.src.computec.inconsistencias.source.InconsistenciasSource;
import com.src.computec.radar.base.ldap.dto.LdapUserDto;
import com.src.computec.radar.base.source.CoreSource;
import com.src.computec.radar.services.utilities.consultas.delegate.ConsultasDelegate;
import com.src.computec.radar.services.utilities.consultas.exception.ConsultasException;
import com.sun.rave.web.ui.appbase.AbstractSessionBean;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.context.FacesContext;

/**
 * <p>Session scope data bean for your application.  Create properties
 *  here to represent cached data that should be made available across
 *  multiple HTTP requests for an individual user.</p>
 *
 * <p>An instance of this class will be created for you automatically,
 * the first time your application evaluates a value binding expression
 * or method binding expression that references a managed bean using
 * this class.</p>
 */
public class SessionIdentInconsistenciaBean extends AbstractSessionBean {
    // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Definition">
    private int __placeholder;
    
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    // </editor-fold>
    private HashMap nodosEM;   
    private List searchResult;
    private String ldapUser;
    
    private boolean renderedMensajeAlertGridPanel=false;
    private boolean renderedBottomGridPanel=false;
    private boolean renderedGuiaImage=false;
    
    private String pathImagenPopup;
    private String codigoProveedor;
    private boolean empresaMensajeria=false;
    
    //PAGINACION
    private boolean paginacion = false;
    private int incrementoIntervalosPaginacion = 0;
    private String numeroRegistrosPaginados = new String();
    private String pagina = new String();
    private int sizeDataTable = 0;
    private int rowsDataTable = 0;
    private int first = 0;
    //FIN PAGINACION

    /** 
     * <p>Construct a new session data bean instance.</p>
     */
    public SessionIdentInconsistenciaBean() {
	try 
        {            
//    this.setLdapUser("fsanchez.computec");     
//    this.codigoProveedor="30";	    
    this.ldapUser = this.getUIdByDomainName(FacesPortletUtil.getPortletRequest(FacesContext.getCurrentInstance()).getRemoteUser());
	    this.codigoProveedor=this.findRolesUser();
            
            
       }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected ApplicationIdentInconsistenciaBean getApplicationIdentInconsistenciaBean() {
        return (ApplicationIdentInconsistenciaBean)getBean("ApplicationIdentInconsistenciaBean");
    }

    /** 
     * <p>This method is called when this bean is initially added to
     * session scope.  Typically, this occurs as a result of evaluating
     * a value binding or method binding expression, which utilizes the
     * managed bean facility to instantiate this bean and store it into
     * session scope.</p>
     * 
     * <p>You may customize this method to initialize and cache data values
     * or resources that are required for the lifetime of a particular
     * user session.</p>
     */
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("SessionBean1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
	InconsistenciasDelegate delegate_=new InconsistenciasDelegate();
	try{
	    this.nodosEM=delegate_.getAllNODO_EM();
	}catch(InconsistenciasException ex){
	    System.out.println("Error:"+ex.getMessage());
	}	
	
	//PAGINACION 
	this.incrementoIntervalosPaginacion = Integer.valueOf(InconsistenciasSource.getInstance().getInconsistenciasSetupKey("incrementoIntervalosPaginacion")).intValue();
        this.numeroRegistrosPaginados = InconsistenciasSource.getInstance().getInconsistenciasSetupKey("incrementoIntervalosPaginacion");
        this.pagina = "1";
        this.rowsDataTable = this.getIncrementoIntervalosPaginacion();
	System.out.println("ROWS:"+rowsDataTable);
	//FIN PAGINACION
    }

    /** 
     * <p>This method is called when the session containing it is about to be
     * passivated.  Typically, this occurs in a distributed servlet container
     * when the session is about to be transferred to a different
     * container instance, after which the <code>activate()</code> method
     * will be called to indicate that the transfer is complete.</p>
     * 
     * <p>You may customize this method to release references to session data
     * or resources that can not be serialized with the session itself.</p>
     */
    public void passivate() {
    }

    /** 
     * <p>This method is called when the session containing it was
     * reactivated.</p>
     * 
     * <p>You may customize this method to reacquire references to session
     * data or resources that could not be serialized with the
     * session itself.</p>
     */
    public void activate() {
    }

    /** 
     * <p>This method is called when this bean is removed from
     * session scope.  Typically, this occurs as a result of
     * the session timing out or being terminated by the application.</p>
     * 
     * <p>You may customize this method to clean up resources allocated
     * during the execution of the <code>init()</code> method, or
     * at any later time during the lifetime of the application.</p>
     */
    public void destroy() {
    }

    public HashMap getNodosEM() {
        return nodosEM;
    }

    public void setNodosEM(HashMap nodosEM) {
        this.nodosEM = nodosEM;
    }
    
    public String getNodosEmDesc(String key){
	return (String)this.nodosEM.get(key);
    }

    public List getSearchResult() {
        return searchResult;
    }

    public void setSearchResult(List searchResult) {
        this.searchResult = searchResult;
    }

    public String getLdapUser() {
        return ldapUser;
    }

    public void setLdapUser(String ldapUser) {
        this.ldapUser = ldapUser;
    }

    public boolean isRenderedMensajeAlertGridPanel() {
        return renderedMensajeAlertGridPanel;
    }

    public void setRenderedMensajeAlertGridPanel(boolean renderedMensajeAlertGridPanel) {
        this.renderedMensajeAlertGridPanel = renderedMensajeAlertGridPanel;
    }

    public boolean isRenderedBottomGridPanel() {
        return renderedBottomGridPanel;
    }

    public void setRenderedBottomGridPanel(boolean renderedBottomGridPanel) {
        this.renderedBottomGridPanel = renderedBottomGridPanel;
    }

    public String getPathImagenPopup() {
        return pathImagenPopup;
    }

    public void setPathImagenPopup(String pathImagenPopup) {
        this.pathImagenPopup = pathImagenPopup;
    }

    public boolean isRenderedGuiaImage() {
        return renderedGuiaImage;
    }

    public void setRenderedGuiaImage(boolean renderedGuiaImage) {
        this.renderedGuiaImage = renderedGuiaImage;
    }
   
   private String getUIdByDomainName(String domainName) {
        String[] tokens =  domainName.split(",");
        if (tokens.length > 0) {
            String[] uIdTokens = (tokens[0]).split("=");
            if (uIdTokens.length == 2) {
                return uIdTokens[1];
            }
        }
        return "";
    }
   
   public String findRolesUser() throws ConsultasException {
        ConsultasDelegate consultasDelegate = new ConsultasDelegate();
	ConstantesGenerales.getInstance();
	String codigoEM=ConstantesGenerales.CODIGO_PROVEEDOR_COMPUTEC;
        LdapUserDto ldapUserDtoAux = new LdapUserDto();
        ldapUserDtoAux.setIdLdap(this.getLdapUser());
        ldapUserDtoAux = consultasDelegate.getLdapUserByPrimaryKey(ldapUserDtoAux);
        //this.ldapUserDto = ldapUserDtoAux;
        Iterator iterRoles = ldapUserDtoAux.getRoles().iterator();	
        while(iterRoles.hasNext()) {
            String nombreRole = (String) iterRoles.next();
            if (nombreRole.compareToIgnoreCase(CoreSource.getInstance().getLdapSetupKey("ldap_rol_empresa_mensajeria_cn")) == 0) {               
		codigoEM=ldapUserDtoAux.getCodigoEmpresaMensajeria();		
		this.setEmpresaMensajeria(true);
            }
        }
	return codigoEM;
    }

    public String getCodigoProveedor() {
        return codigoProveedor;
    }

    public void setCodigoProveedor(String codigoProveedor) {
        this.codigoProveedor = codigoProveedor;
    }

    public boolean isEmpresaMensajeria() {
        return empresaMensajeria;
    }

    public void setEmpresaMensajeria(boolean empresaMensajeria) {
        this.empresaMensajeria = empresaMensajeria;
    }

    public boolean isPaginacion() {
        return paginacion;
    }

    public void setPaginacion(boolean paginacion) {
        this.paginacion = paginacion;
    }

    public int getIncrementoIntervalosPaginacion() {
        return incrementoIntervalosPaginacion;
    }

    public void setIncrementoIntervalosPaginacion(int incrementoIntervalosPaginacion) {
        this.incrementoIntervalosPaginacion = incrementoIntervalosPaginacion;
    }

    public String getNumeroRegistrosPaginados() {
        return numeroRegistrosPaginados;
    }

    public void setNumeroRegistrosPaginados(String numeroRegistrosPaginados) {
        this.numeroRegistrosPaginados = numeroRegistrosPaginados;
    }

    public String getPagina() {
        return pagina;
    }

    public void setPagina(String pagina) {
        this.pagina = pagina;
    }

    public int getSizeDataTable() {
        return sizeDataTable;
    }

    public void setSizeDataTable(int sizeDataTable) {
        this.sizeDataTable = sizeDataTable;
    }

    public int getRowsDataTable() {
        return rowsDataTable;
    }

    public void setRowsDataTable(int rowsDataTable) {
        this.rowsDataTable = rowsDataTable;
    }

    public int getFirst() {
        return first;
    }

    public void setFirst(int first) {
        this.first = first;
    }
}
